package com.hs.app.dwr;

import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.directwebremoting.annotations.Param;
import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.directwebremoting.spring.SpringCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.hs.app.helper.DwrResponseHelper;
import com.hs.app.model.CdcBlfy;
import com.hs.app.model.CdcBlfybd;
import com.hs.app.model.CdcFjZyZfYlzy;
import com.hs.app.model.CdcHerb;
import com.hs.app.model.CdcJsff;
import com.hs.app.model.CdcLhyy;
import com.hs.app.model.CdcLincdz;
import com.hs.app.model.CdcLinchuang;
import com.hs.app.model.CdcLxzb;
import com.hs.app.model.CdcPrehreb;
import com.hs.app.model.CdcSyqk;
import com.hs.app.model.CdcZfBwCf;
import com.hs.app.service.IProduceService;
import com.hs.core.cache.DictCacheUtil;
import com.hs.core.dwr.DwrHelper;

/**
 * 数据加工DWR处理类
 * @author Abdi
 */
@Controller
@RemoteProxy(creator = SpringCreator.class, name = "produceHelper" ,
	creatorParams = @Param(name = "beanName", value = "produceHelper"))
public class ProduceHelper extends DwrHelper{
	private IProduceService produceService;

	/**
	 * 临床观察信息保存更新
	 * @param linchuang
	 * @return
	 */
	@RemoteMethod
	public String saveOrUpdateLinchuanggc(CdcLinchuang linchuang,String formId){
		int operator = produceService.saveOrUpdate(linchuang);
		return DwrResponseHelper.getSaveUpdateResponseMsg(linchuang,formId,operator);
	}
	
	/**
	 * 联合用药信息保存更新
	 * @param lhyy
	 * @return
	 */
	@RemoteMethod
	public String saveOrUpdateLianheyy(CdcLhyy lhyy,String formId){
		int operator = produceService.saveOrUpdate(lhyy);
		return DwrResponseHelper.getSaveUpdateResponseMsg(lhyy,formId,operator);
	}
	
	/**
	 * 不良反应信息保存更新
	 * @param lhyy
	 * @return
	 */
	@RemoteMethod
	public String saveOrUpdateBuliangfyxx(CdcBlfy blfyxx,String formId){
		int operator = produceService.saveOrUpdate(blfyxx);
		return DwrResponseHelper.getSaveUpdateResponseMsg(blfyxx,formId,operator);
	}
	
	/**
	 * 疗效指标信息保存更新
	 * @param lxzb
	 * @return
	 */
	@RemoteMethod
	public String saveOrUpdateLiaoxiaozb(CdcLxzb lxzb,String formId){
		int operator = produceService.saveOrUpdate(lxzb);
		return DwrResponseHelper.getSaveUpdateResponseMsg(lxzb,formId,operator);
	}
	
	/**
	 * 临床对照信息保存更新
	 * @param lxzb
	 * @return
	 */
	@RemoteMethod
	public String saveOrUpdateLinchuangdz(CdcLincdz lcdz,String formId){
		int operator = produceService.saveOrUpdate(lcdz);
		return DwrResponseHelper.getSaveUpdateResponseMsg(lcdz,formId,operator);
	}
	
	/**
	 * 实验情况信息保存更新
	 * @param lxzb
	 * @return
	 */
	@RemoteMethod
	public String saveOrUpdateShiyanqk(CdcSyqk syqk,String formId){
		int operator = produceService.saveOrUpdate(syqk);
		return DwrResponseHelper.getSaveUpdateResponseMsg(syqk,formId,operator);
	}
	
	/**
	 * 方剂-中药-组分药理作用信息保存更新
	 * @param ylzy
	 * @return
	 */
	@RemoteMethod
	public String saveOrUpdateYaolizy(CdcFjZyZfYlzy ylzy,String formId){
		int operator = produceService.saveOrUpdate(ylzy);
		return DwrResponseHelper.getSaveUpdateResponseMsg(ylzy,formId,operator);
	}
	
	/**
	 * 组分-部位-（活性）成分保存更新
	 * @param hxcf
	 * @return
	 */
	@RemoteMethod
	public String saveOrUpdateCdcHuoxingcf(CdcZfBwCf hxcf,String formId){
		int operator = produceService.saveOrUpdate(hxcf);
		return DwrResponseHelper.getSaveUpdateResponseMsg(hxcf,formId,operator);
	}
	
	
	/**
	 * 技术方法信息保存更新
	 * @param ylzy
	 * @return
	 */
	@RemoteMethod
	public String saveOrUpdateJishuff(CdcJsff jsff,String formId){
		int operator = produceService.saveOrUpdate(jsff);
		return DwrResponseHelper.getSaveUpdateResponseMsg(jsff,formId,operator);
	}
	
	/**
	 * 中药成药信息保存更新
	 * @param zycy
	 * @return
	 */
	@RemoteMethod
	public String saveOrUpdateZhongyaocy(CdcPrehreb zycy,String formId){
		int operator = produceService.saveOrUpdate(zycy);
		return DwrResponseHelper.getSaveUpdateResponseMsg(zycy,formId,operator);
	}
	
	/**
	 * 中药组成信息保存更新
	 * @param zyzz
	 * @return
	 */
	@RemoteMethod
	public String saveOrUpdateZhongyaozz(CdcHerb zyzz,String formId){
		int operator = produceService.saveOrUpdate(zyzz);
		return DwrResponseHelper.getSaveUpdateResponseMsg(zyzz,formId,operator);
	}

	/**
	 * 不良反应报道信息保存更新
	 * @param zyzz
	 * @return
	 */
	@RemoteMethod
	public String saveOrUpdateBulianfybd(CdcBlfybd blfybd,String formId){
		int operator = produceService.saveOrUpdate(blfybd);
		return DwrResponseHelper.getSaveUpdateResponseMsg(blfybd,formId,operator);
	}
	

	/**
	 * 按主键获取数据(获取一条数据)
	 * @param formId 表单
	 * @param refrenceId 题录主键
	 * @param selfId 主键
	 * @return
	 */
	@RemoteMethod
	public String getObjectByPK(String formId,String selfId){
		Class<?> clazz = DwrResponseHelper.getObjectClazz(formId);
		Object obj = produceService.findByID(clazz, Long.parseLong(selfId));
		return DwrResponseHelper.getOneObjResponse(obj, formId);
	}
	
	/**
	 * 按题录主键获取数据
	 * @param formId
	 * @param refrenceId
	 * @return
	 */
	@RemoteMethod
	public String getObjectListByRefrence(String formId,String refrenceId){
		Class<?> clazz = DwrResponseHelper.getObjectClazz(formId);
		List<Object> list = null;
		// 不良反应报道 活性组分信息需同时设定rtype为1的获取数据
		if("blfybd".equals(formId) || "hxcf".equals(formId)){
			list = produceService.findObjectList(clazz, new String[]{"pid","rtype"}, new String[]{refrenceId,"1"});
		}else{
			list = produceService.findObjectList(clazz, new String[]{"pid"}, new String[]{refrenceId});
		}
		return DwrResponseHelper.getListObjResponse(list, formId);
	}
	
	/**
	 * 按题录主键、外键获取数据集合(获取直接主表的附表)
	 * @param formId
	 * @param refrenceId
	 * @param foreignId
	 * @return
	 */
	@RemoteMethod
	public String getObjectListByFK(String formId,String refrenceId,String foreignId){
		Class<?> clazz = DwrResponseHelper.getObjectClazz(formId);
		String[] params = DwrResponseHelper.getKeyArray(formId);
		String[] values = null;
		
		if("sylxzb".equals(formId)){
			values = new String[]{refrenceId,foreignId,"1"};
		}else if("lxzb".equals(formId) || "zblfybd".equals(formId) || "zhxcf".equals(formId)){
			values = new String[]{refrenceId,foreignId,"2"};
		}else{
			values = new String[]{refrenceId,foreignId};
		}
		List<Object> list = produceService.findObjectList(clazz, params, values);
		return DwrResponseHelper.getListObjResponse(list, formId);
	}
	
	/**
	 * 删除数据
	 * @param formId
	 * @param selfId
	 */
	@RemoteMethod
	public String deleteOneRowData(String formId,String selfId,String refrenceId){
		produceService.deleteCascadeTable(formId, selfId, refrenceId);
		return DwrResponseHelper.getDelResponseMsg(formId);
	}
	
	/**
	 * 预备需要的字典集
	 * @param dicCode
	 * @return
	 */
	@RemoteMethod
	public String getDictData(String[] dicCode){
		DictCacheUtil cache = DictCacheUtil.getInst();
		Map<String,List<Map<String,String>>> dataMap = cache.getDictData();
		JSONArray result = new JSONArray();
		for(String code : dicCode){
			if(dataMap.containsKey(code)){
				List<Map<String,String>> list = dataMap.get(code); // 前台需要的字典集

				JSONObject obj = new JSONObject();
				
				obj.put("dicttype", code);
				JSONArray array = new JSONArray();
				for(Map<String,String> map : list){
					JSONObject o = new JSONObject();
					o.put("dictcode", map.get("DICTCODE"));
					o.put("dictname", map.get("DICTNAME"));
					array.add(o);
				}
				obj.put("dictlist", array);
				result.add(obj);
			}
		}
		if(null == result){
			return "";
		}
		return result.toString();
	}
	
	@Autowired  
	public void setIProduceService(IProduceService produceService) {
		this.produceService = produceService;
	}
}